package com.subang.function.controller;

import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.core.domain.model.LoginUser;
import com.subang.common.core.page.TableDataInfo;
import com.subang.common.enums.BusinessType;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.utils.uuid.IdUtils;
import com.subang.function.domain.ShenlunAnswerLike;
import com.subang.function.domain.ShenlunReferenceAnswers;
import com.subang.function.service.IShenlunAnswerLikeService;
import com.subang.function.service.IShenlunReferenceAnswersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 申论答案点赞Controller
 * 
 * @author subei
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/compare/like")
public class ShenlunAnswerLikeController extends BaseController
{
    @Autowired
    private IShenlunAnswerLikeService shenlunAnswerLikeService;

    @Autowired
    private IShenlunReferenceAnswersService shenlunReferenceAnswersService;

    /**
     * 查询申论答案点赞列表
     */
    @PreAuthorize("@ss.hasPermi('compare:like:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShenlunAnswerLike shenlunAnswerLike)
    {
        startPage();
        List<ShenlunAnswerLike> list = shenlunAnswerLikeService.selectShenlunAnswerLikeList(shenlunAnswerLike);
        return getDataTable(list);
    }

    /**
     * 导出申论答案点赞列表
     */
    @PreAuthorize("@ss.hasPermi('compare:like:export')")
    @Log(title = "申论答案点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShenlunAnswerLike shenlunAnswerLike)
    {
        List<ShenlunAnswerLike> list = shenlunAnswerLikeService.selectShenlunAnswerLikeList(shenlunAnswerLike);
        ExcelUtil<ShenlunAnswerLike> util = new ExcelUtil<ShenlunAnswerLike>(ShenlunAnswerLike.class);
        util.exportExcel(response, list, "申论答案点赞数据");
    }


    @PreAuthorize("@ss.hasPermi('compare:like:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(shenlunAnswerLikeService.selectShenlunAnswerLikeById(id));
    }

    @Log(title = "申论答案点赞", businessType = BusinessType.INSERT)
    @PostMapping(value = "/{answer_id}")
    public AjaxResult answerLike(@PathVariable("answer_id") String answer_id)
    {
        ShenlunReferenceAnswers shenlunReferenceAnswers = shenlunReferenceAnswersService.selectShenlunReferenceAnswersById(answer_id);
        shenlunReferenceAnswers.setLikeCount(shenlunReferenceAnswers.getLikeCount()+1);
        shenlunReferenceAnswersService.updateShenlunReferenceAnswers(shenlunReferenceAnswers);

        ShenlunAnswerLike shenlunAnswerLike =  new ShenlunAnswerLike();
        shenlunAnswerLike.setQuestionId(shenlunReferenceAnswers.getQuestionId());

        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        shenlunAnswerLike.setUserId(String.valueOf(user.getUserId()));
        List<ShenlunAnswerLike> list = shenlunAnswerLikeService.selectShenlunAnswerLikeList(shenlunAnswerLike);
        
        if (list.isEmpty()){
            shenlunAnswerLike.setId(IdUtils.simpleUUID());
            shenlunAnswerLike.setAnswerId(answer_id);
            shenlunAnswerLike.setLikeTime(new Date());

            return toAjax(shenlunAnswerLikeService.insertShenlunAnswerLike(shenlunAnswerLike));
        }else{
            shenlunAnswerLike = list.get(0);
            ShenlunReferenceAnswers shenlunReferenceAnswers1 = shenlunReferenceAnswersService.selectShenlunReferenceAnswersById(shenlunAnswerLike.getAnswerId());
            shenlunReferenceAnswers1.setLikeCount(shenlunReferenceAnswers1.getLikeCount()-1);
            shenlunReferenceAnswersService.updateShenlunReferenceAnswers(shenlunReferenceAnswers1);

            shenlunAnswerLike.setAnswerId(answer_id);
            shenlunAnswerLike.setLikeTime(new Date());
            return toAjax(shenlunAnswerLikeService.updateShenlunAnswerLike(shenlunAnswerLike));
        }
    }

    /**
     * 新增申论答案点赞
     */
    @PreAuthorize("@ss.hasPermi('compare:like:add')")
    @Log(title = "申论答案点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShenlunAnswerLike shenlunAnswerLike)
    {
        return toAjax(shenlunAnswerLikeService.insertShenlunAnswerLike(shenlunAnswerLike));
    }



    /**
     * 修改申论答案点赞
     */
    @PreAuthorize("@ss.hasPermi('compare:like:edit')")
    @Log(title = "申论答案点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShenlunAnswerLike shenlunAnswerLike)
    {
        return toAjax(shenlunAnswerLikeService.updateShenlunAnswerLike(shenlunAnswerLike));
    }

    /**
     * 删除申论答案点赞
     */
    @PreAuthorize("@ss.hasPermi('compare:like:remove')")
    @Log(title = "申论答案点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(shenlunAnswerLikeService.deleteShenlunAnswerLikeByIds(ids));
    }
}
